package org.shoukaiseki.test.spring.multiple.datasource;

import org.shoukaiseki.test.spring.multiple.datasource.config.constant.BeanConstants;
import org.shoukaiseki.test.spring.multiple.datasource.config.mappers.Test001OneMapper;
import org.shoukaiseki.test.spring.multiple.datasource.config.mappers.Test001TwoMapper;
import org.shoukaiseki.test.spring.multiple.datasource.config.model.Test001;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class TransactionController {

    @Resource
    Test001OneMapper test001OneMapper;

    @Resource
    Test001TwoMapper test001TwoMapper;


    @RequestMapping( value="/transa1")
    @ResponseBody
    @Transactional(transactionManager = BeanConstants.ONE_DATASOURCE+"Transaction"
            ,rollbackFor = NullPointerException.class)
    public Map<String, String> transa1(){
        Test001 test001=new Test001();
        Map<String, String> result = new HashMap<>();

        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        if(1==1){
            throw new NullPointerException("\n\n...[Transaction error]...");
        }
        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);

        result.put("status","0");
        result.put("count",""+ test001OneMapper.count());

        return result;
    }

    @RequestMapping( value="/transa1a")
    @ResponseBody
    @Transactional(transactionManager = BeanConstants.ONE_DATASOURCE+"Transaction"
            ,rollbackFor = NullPointerException.class)
    public Map<String, String> transa1a(){
        Test001 test001=new Test001();
        Map<String, String> result = new HashMap<>();

        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);

        result.put("status","0");
        result.put("count",""+ test001OneMapper.count());

        return result;
    }

    @RequestMapping( value="/transa1b")
    @ResponseBody
    @Transactional(transactionManager = BeanConstants.TWO_DATASOURCE+"Transaction"
            ,rollbackFor = NullPointerException.class)
    public Map<String, String> transa1b(){
        Test001 test001=new Test001();
        Map<String, String> result = new HashMap<>();

        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);

        if(1==1){
            throw new NullPointerException("\n\n...[Transaction error]...");
        }

        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);

        result.put("status","0");
        result.put("count",""+ test001OneMapper.count());

        return result;
    }

    @RequestMapping( value="/count")
    @ResponseBody
    public Map<String, String> count(){
        Map<String, String> result = new HashMap<>();
        result.put("onedb_count",""+ test001OneMapper.count());
        result.put("twodb_count",""+ test001TwoMapper.count());
        return result;
    }



    @RequestMapping( value="/delall")
    @ResponseBody
    public Map<String, String> delall(){
        Map<String, String> result = new HashMap<>();
        result.put("onedb_count_del_before",""+ test001OneMapper.count());
        result.put("twodb_count_del_before",""+ test001TwoMapper.count());
        test001OneMapper.delall();
        test001TwoMapper.delall();
        result.put("onedb_count_del_after",""+ test001OneMapper.count());
        result.put("twodb_count_del_after",""+ test001TwoMapper.count());
        return result;
    }



    @RequestMapping( value="/transa12")
    @ResponseBody
    @Transactional(transactionManager = BeanConstants.TWO_DATASOURCE+"Transaction"
            ,rollbackFor = NullPointerException.class)
    public Map<String, String> transa12(){
        Test001 test001=new Test001();
        Map<String, String> result = new HashMap<>();

        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001TwoMapper.insert(test001);
        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001TwoMapper.insert(test001);

        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001TwoMapper.insert(test001);

        result.put("status","0");
        result.put("count",""+ test001OneMapper.count());

        return result;
    }

    @RequestMapping( value="/transa12a")
    @ResponseBody
    @Transactional(transactionManager = BeanConstants.TWO_DATASOURCE+"Transaction"
            ,rollbackFor = NullPointerException.class)
    public Map<String, String> transa12a(){
        Test001 test001=new Test001();
        Map<String, String> result = new HashMap<>();

        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001TwoMapper.insert(test001);
        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001TwoMapper.insert(test001);

        if(1==1){
            throw new NullPointerException("\n\n...[Transaction error]...");
        }

        test001.setName(UUID.randomUUID().toString());
        test001OneMapper.insert(test001);
        test001TwoMapper.insert(test001);

        result.put("status","0");
        result.put("count_1",""+ test001OneMapper.count());
        result.put("count_2",""+ test001OneMapper.count());

        return result;
    }

}
